package TUILayer;
import java.util.Scanner;
import ControlLayer.*;
/**
 * User interface to the DVD controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 */

public class DvdUI
{
    private DVDCtr dvdCtr;//DVD UI must know the DVD controller 
    
    public DvdUI()
    {
        dvdCtr = DVDCtr.getInstance();
    }
    
    public void start()
    {
        //dvdCtr= ctr;
        dvdMenu();
    }
    
    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choice =   writeDVDMenu();
            if (choice == 1)
            {
                findDVD();
            }
            else{if(choice == 2){
                   createDVD();
                }
                else{
                    if(choice == 3){
                        createCopy();
                    }
                    else{
                        if(choice == 4){
                             deleteDVD();
                        }
                        else{
                            if(choice == 5){
                                listAllDvds();
                            }
                            else{
                                exit = true;
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
        }//end while
    }
    private int writeDVDMenu()
    {
           // creates an object keyboard to read data from the keyboard
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** DVD Menu ***");
            System.out.println(" (1) Show DVD information");
            System.out.println(" (2) Create DVD description");
            System.out.println(" (3) Create DVD copy");
            System.out.println(" (4) Delete DVD");
            System.out.println(" (5) Show current DVD list");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
    }
    
    private void continueInput()
    {
        System.out.println("\nPress enter to continue");
        new Scanner(System.in).nextLine();
    }
    
    private long inputDVDId()
    {
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write the Id of the DVD:  ");
           try{
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        } //end while       
        return id;
    }
        
    private String inputTitle()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD title: " );
        String title = keyboard.nextLine();
        return title;
    }
    
    private String inputGenre()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVDs genre: " );
        String genre = keyboard.nextLine();
        return genre;
    }
    
    private String inputDirector()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVDs director: " );
        String director = keyboard.nextLine();
        return director;
    }
    
    private String inputYear()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVDs year of production: " );
        String year = keyboard.nextLine();
        return year;
    }
    
    /**
     * Prints information about a Dvd object
     */
    private void findDVD()
    {     
       long id = inputDVDId();

       if (!dvdCtr.validId(id)) {
            System.out.println("Unknown id");
       } else {
            System.out.println("Title: " + dvdCtr.findDVD(id).getTitle());
            System.out.println("Year: " + dvdCtr.findDVD(id).getYear());
            System.out.println("Genre: " + dvdCtr.findDVD(id).getGenre());
            System.out.println("Director: " + dvdCtr.findDVD(id).getDirector());
            System.out.println();
            System.out.println("Copies:");
            dvdCtr.listAllDVDCopies(id);

       }
       continueInput();
    }
    
    /**
     * Creates a Dvd object
     */
    private void createDVD()
    {
        String title = inputTitle();
        String genre = inputGenre();
        String director = inputDirector();
        String year = inputYear();
        dvdCtr.createDVD(title, genre, director, year);
        continueInput();
    }
    
    /**
     * Creates a DvdSpecimen object
     */
    private void createCopy()
    {
        long dvdId = inputDVDId();
        dvdCtr.createCopy(dvdId);
        System.out.println("The copy was successfully created");
        continueInput();
    }
    
    /**
     * Deletes a Dvd object
     */
    private void deleteDVD()
    {
       long id = inputDVDId();

       if (!dvdCtr.validId(id)) {
            System.out.println("Unknown id");
            continueInput();
       } else {
            dvdCtr.deleteDVD(id);
            System.out.println("The DVD with the id: " + id + " was successfully deleted");
            continueInput();

       }
    }
    
    /**
     * List all Dvd objects
     */
    private void listAllDvds()
    {
        dvdCtr.listAllDVDs();
        continueInput();
    }
}